import axios from "axios";

export default {
  post(data, fnSuccess, fnError) {
    const success = function (res) {
      if (res.data.token && res.data.token != '') {
        setLoginInfo(data);
      }
      if (res.data.responseCode == '9998' || res.data.responseMsg == '用户未登录') {
        clearStorage('token');
      }
      fnSuccess && fnSuccess(res);
    };
    const error = function (res) {
      fnError && fnError(res);
    };
    return axios({
      method: 'post',
      data: data,
    }).then(success).then(error)
  },
  get(params, fnSuccess, fnError) {
    const success = function (res) {
      if (res.data.token && res.data.token != '') {
        setLoginInfo(data);
      }
      if (res.data.responseCode == '9998' || res.data.responseMsg == '用户未登录') {
        clearStorage('token');
      }
      fnSuccess && fnSuccess(res);
    };
    const error = function (res) {
      fnError && fnError(res);
    };
    return axios({
      method: 'get',
      params,
    }).then(success).then(error)
  },

  autoLogin() {
    return autoLogin();
  },
  getRequestData(biz, jsonData) {
    return getRequestData(biz, jsonData)
  },

  setLoginInfo(data) {
    return setLoginInfo(data)
  },
  getLoginInfo(key) {
    return getLoginCustomer(key)
  },
  getLoginCustomer() {
    return JSON.parse(getStorage('customer') || '{}');
  },
  setLoginCustomer(customer) {
    return setLoginCustomer(customer)
  },
  logout(action) {
    return logout(action)
  },
  setTempStorage(key, value) {
    return setTempStorage(key, value)
  },
  setStorage(key, value) {
    return setStorage(key, value)
  },
  getTempStorage(key) {
    return getTempStorage(key)
  },
  getStorage(key) {
    return getStorage(key)
  },
  clearTempStorage(key) {
    return clearStorage(key)
  },

  clearStorage(key) {
    return clearStorage(key)
  },

  checkLogin() {
    return checkLogin()
  },

  getQueryString(name) {
    return getQueryString(name)
  },
  isMobile(mobile) {
    return isMobile(mobile)
  }
}

/**
 * 自动登录
 */
function autoLogin() {
  if (getQueryString('token')) {
    setLoginInfo({
      token: getQueryString('token'), subscribe: getQueryString('subscribe')
    });

    this.defaults().post(getRequestData("getUserInfo", {
      id: getQueryString("userId") || 0
    }), function (data) {
      setStorage("user", JSON.stringify(data.data.user));
    });
  }
}

/**
 * 拼接请求参数
 * @param biz
 * @param jsonData
 */
function getRequestData(biz, jsonData) {
  jsonData = jsonData || {};
  const data = {
    version: "3.0", bizType: biz, token: getLoginInfo('token') || '', platform: 2, data: jsonData
  };
  return JSON.stringify(data);
}

/**
 * 设置登录信息
 * @param data
 */
function setLoginInfo(data) {
  if (data.token) {
    localStorage.setItem('token', data.token);
  }
}

/**
 * 获取登录信息
 * @param key
 * @returns {*}
 */
function getLoginInfo(key) {
  return localStorage.getItem(key);
}

/**
 * 获取登录客户
 */
function getLoginCustomer() {
  return JSON.parse(getStorage('customer') || '{}');
}

/**
 * 设置客户
 * @param customer
 */
function setLoginCustomer(customer) {
  setStorage('customer', JSON.stringify(customer));
}

/**
 * 退出登录
 */
function logout(action) {
  clearStorage('token');
  clearStorage('customer');
  var type = typeof action;
  if (type == 'string') {
    location.href = action;
  }
  if (type == 'function') {
    action();
  }
}
/**
 * 设置临时变量
 * @param key
 * @param value
 */
function setTempStorage(key, value) {
  sessionStorage.setItem(key, value);
}

/**
 * 设置变量
 * @param key
 * @param value
 */
function setStorage(key, value) {
  localStorage.setItem(key, value);
}

/**
 * 获取临时变量
 * @param key
 * @param isClear
 * @returns {*}
 */
function getTempStorage(key) {
  return sessionStorage.getItem(key);
}

/**
 * 获取变量
 * @param key
 * @param isClear
 * @returns {*}
 */
function getStorage(key) {
  return localStorage.getItem(key);
}

/**
 * 清楚临时变量
 * @param key
 */
function clearTempStorage(key) {
  if (key !== undefined) {
    sessionStorage.removeItem(key);
  } else {
    sessionStorage.clear();
  }
}

/**
 * 清楚变量
 * @param key
 */
function clearStorage(key) {
  if (key !== undefined) {
    localStorage.removeItem(key);
  } else {
    localStorage.clear();
  }
}

/**
 * 校验是否成功
 */
function checkLogin() {
  var token = getLoginInfo('token');
  if (!token) {
    return false;
  }
  return !!token;
}

/**
 * 获取url查询参数
 * @param name
 * @returns {*}
 */
function getQueryString(name) {
  const reg = new RegExp("(\\?|&)" + name + "=(.*?)(&|$)", "i");
  /*(^|&)" + name + "=([^&]*)(&|$)*/
  /*window.location.hash*/   //by zsx
  const matchs = window.location.hash.substr(1).match(reg);
  if (matchs != null) {
    return decodeURI(decodeURIComponent(matchs[2]));
  }
  return null;
}

/**
 * 是否是合法的手机号
 * @param mobile
 * @returns {boolean}
 */
function isMobile(mobile) {
  return /^1[3578]\d{9}$/.test(mobile);
}
